'use strict';


const db = uniCloud.database();
const dbCmd = db.command // 取指令

async function orderChange(event){
	let { id , type } = event
	console.log(id , type)
	let updateOrderResult = await db.collection('order').doc(id).update({
		status: type
	})
	
	if(type == 1){
		let orderData = await db.collection('order').doc(id).field({'hotel_id':true}).get()
		console.log(orderData)
		let {hotel_id} = orderData.data[0]
		await db.collection('hotel').doc(hotel_id ).update({
			order_count:dbCmd.inc(1)
		})
	}
	console.log(updateOrderResult.affectedDocs)
	if(updateOrderResult.affectedDocs == 1){
		return {
			code: 0,
			msg:'更新成功'
		}
	}else{
		return {
			code: 10000,
			msg:'系统错误，请稍后重试'
		}
	}
	
}

exports.main = orderChange;